-- ~~| Libraries |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local DiesalGUI 	= LibStub('DiesalGUI-1.0')
local DiesalStyle = LibStub("DiesalStyle-1.0")
local DiesalTools = LibStub('DiesalTools-1.0')
-- ~~| Lua Upvalues |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local tostring										= tostring
local format 										= string.format
-- ~~| WoW Upvalues |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~| ConfigToggle |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local Type 		= 'OPTselect'
local Version 	= 1
-- ~~| ConfigToggle StyleSheets |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local styleSheet = {
	['name-color'] = {
		type			= 'Font',
		color			= 'bfbfbf',
	},	
}
local dropdownStyleSheet = {
	['frame-arrow'] = {
		type			= 'texture',
		layer			= 'BORDER',
		alpha 		= 0,
	},
}
-- ~~| ConfigToggle Locals |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~| ConfigToggle Methods |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local methods = {
	['OnAcquire'] = function(self)
		self:AddStyleSheet(styleSheet)
		-- self:AddStyleSheet(wireFrameSheet)
		self:Show()
		self.checkBox:Hide()
	end,
	['OnRelease'] = function(self)

	end,
	['ApplySettings'] = function(self)
		local optionTable = self.settings.optionTable
		self:SetHeight(18)
		if self.settings.position == 1 then
			self:SetPoint('TOPLEFT',0,-self.settings.padTop)
			self:SetPoint('RIGHT')
		else
			self:SetPoint('TOPLEFT',self.settings.parentObject.children[self.settings.position-1].frame,'BOTTOMLEFT',0,0)
			self:SetPoint('RIGHT')
		end

		self.name:SetText(optionTable.name)
		if optionTable.toggle ~= false then self.checkBox:Show() end				
		self.selecter:SetSettings({ width = optionTable.width and DiesalTools:Round(optionTable.width,2)-1 or 50 },true)	
		self.selecter:SetList(optionTable.values)				
	end,
	['SetDB'] = function(self,db,moduleDB)
		self.settings.db 			= db
		self.settings.moduleDB 	= moduleDB	
		
		if self.settings.optionTable.toggle ~= false then
			self.checkBox:SetChecked(db.enable)
			if self.settings.optionTable.set then self.settings.optionTable.set('toggle',db.enable) end
			self:FireEvent('OnOptionUpdate',self.settings.optionTable.name,'toggle',db.enable)		
		end
		self.selecter:SetValue(db.value)		
		if self.settings.optionTable.set then self.settings.optionTable.set('select',db.value) end	
		self:FireEvent('OnOptionUpdate',self.settings.optionTable.name,'select',db.value)			
	end,
	['Lock'] = function(self,lock)
		self.checkBox:Disable()
		self.selecter:EnableMouse(false)
	end,
	['Unlock'] = function(self,lock)
		self.checkBox:Enable()
		self.selecter:EnableMouse(true)
	end,
}
-- ~~| ConfigToggle Constructor |~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
local function Constructor()
	local self 		= DiesalGUI:CreateObjectBase(Type)
	local frame		= CreateFrame('Frame',nil,UIParent)
	self.frame		= frame
	-- ~~ Default Settings ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	self.defaults = { }
	-- ~~ Registered Events ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	-- ~~ Construct ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	local checkBox = DiesalGUI:Create('CheckBox')
	checkBox:SetParent(frame)
	checkBox:SetPoint("TOPLEFT",2,-3)
	checkBox:SetEventListener('OnValueChanged', function(this,event,value)
		self.settings.db.enable = value
		if self.settings.optionTable.set then self.settings.optionTable.set('toggle',value)	end
		self:FireEvent('OnOptionUpdate',self.settings.optionTable.name,'toggle',value)		
	end)
	checkBox:SetEventListener('OnEnter', function(this)
		if not self.settings.moduleDB.tooltips then return end
		GameTooltip:SetOwner(checkBox.frame, "ANCHOR_TOPLEFT",0,2)
		GameTooltip:AddLine('enable option')
		GameTooltip:Show()
	end)
	checkBox:SetEventListener('OnLeave', function(this)
		GameTooltip:Hide()
	end)	
	
	local selecter = DiesalGUI:Create('Dropdown')
	selecter:AddStyleSheet(dropdownStyleSheet) 
	selecter:SetParent(frame)
	selecter:SetPoint('TOPRIGHT',-3,-1)	
	selecter.text:SetPoint("BOTTOMRIGHT", -4, -1)
	selecter.text:SetJustifyH("CENTER")			
	selecter:SetEventListener('OnValueSelected', function(this,event,key)		
		self.settings.db.value = key					
		if self.settings.optionTable.set then self.settings.optionTable.set('select',key) end
		self:FireEvent('OnOptionUpdate',self.settings.optionTable.name,'select',key)	
				
	end)
	selecter:SetEventListener('OnEnter', function(this)		
		if not self.settings.moduleDB.tooltips then return end
		GameTooltip:SetOwner(selecter.frame, "ANCHOR_TOPRIGHT",0,2)		
		GameTooltip:AddLine('Select an option')	
		GameTooltip:Show()
	end)
	selecter:SetEventListener('OnLeave', function(this)			
		GameTooltip:Hide()
	end)	

	local nameFrame = self:CreateRegion("Frame", 'nameFrame', frame)
	nameFrame:SetHeight(14)
	nameFrame:SetPoint("TOPLEFT",16,-2)
	nameFrame:SetPoint("RIGHT",selecter.frame,'LEFT',-4,0)
	nameFrame:SetScript('OnEnter', function(this)
		if not self.settings.moduleDB.configTooltips then return end
		if not self.settings.optionTable.tooltip then return end
		GameTooltip:SetOwner(nameFrame, "ANCHOR_TOPLEFT",0,2)
		GameTooltip:AddLine('|cffff7f00'..self.settings.optionTable.tooltip,nil,nil,nil,1)
		GameTooltip:Show()
	end)
	nameFrame:SetScript('OnLeave', function(this)
		GameTooltip:Hide()
	end)

	local name = self:CreateRegion("FontString", 'name', nameFrame)
	name:SetPoint("TOPLEFT",0,-2)
	name:SetPoint("BOTTOMRIGHT",4,-2)
	name:SetJustifyV("TOP")
	name:SetJustifyH("LEFT")
	name:SetWordWrap(false)

	-- ~~ Frames ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	self.checkBox 	= checkBox
	self.selecter	= selecter
	-- ~~ Methods ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	for method, func in pairs(methods) do
		self[method] = func
	end
	-- ~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	DiesalGUI:RegisterObject(self)
	return self
end
DiesalGUI:RegisterObjectConstructor(Type,Constructor,Version)